﻿namespace MsForgeReboot.Web.Areas.ControlPanel.Controllers {
    #region Usings

    using System.Web.Mvc;
    using AutoMapper;
    using MsForgeReboot.Framework.Entities;
    using MsForgeReboot.Framework.Interfaces;
    using MsForgeReboot.Framework.ViewModels;
    using MsForgeReboot.Web.Attributes;

    #endregion

    public class EventController : Controller {
        private readonly IEventService eventService;

        private readonly IUserService userService;

        public EventController(IEventService eventService, IUserService userService) {
            this.eventService = eventService;
            this.userService = userService;
        }

        public ActionResult Index(int page = 1, int pageSize = 10) {
            return this.View(this.eventService.UpcomingEvents(page, pageSize));
        }

        public ActionResult Create() {
            return this.View("Edit", new EventEditModel());
        }

        [HttpPost]
        public ActionResult Edit(EventEditModel model) {
         
            if (ModelState.IsValid) {
                model.Author.Id = userService.GetSingleUser(User.Identity.Name).Id;
                this.eventService.Save(Mapper.Map<Event>(model));
                return this.RedirectToAction("Index");
            }

            return this.View("Edit", model);
        }

        [HttpPost]
        public ActionResult Delete(string urlPointer) {
            this.eventService.Delete(urlPointer);
            return Json("success", JsonRequestBehavior.DenyGet);
        }

        public ActionResult Edit(string urlPointer) {
            return this.View("Edit", Mapper.Map<EventEditModel>(this.eventService.GetByUrlPointer(urlPointer)));
        }
    }
}